<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Constructor{
	 var $htmlService;
	 var $phpConstructor;
	 var $phpService;
	 var $modules;
	 
	 
	 
	 function Constructor(){
	 	  
 	$this->phpConstructor=array('tabla'						=>NULL,
 								'cabecerasTabla'			=>NULL,
								'valoresTabla'				=>NULL,
								'otherService'				=>NULL,
								'validaciones'				=>NULL,
								'selectValores'				=>NULL,
								'form'						=>NULL,
								'inicializacion'			=>NULL,
								'inicializacionEditDelete'	=>NULL,
								'saveAddEdit'				=>NULL,
								'saveDelete'				=>NULL);
								
	$this->phpService=array(	'tabla'						=>NULL,
 								'dqlReturnData'				=>NULL,
								'dqlReturnSelect'			=>NULL,
								'dqlReturnList'				=>NULL);
								
		
	 	
	 }
	 
	 function modulesCreate(){
		 $CI =& get_instance();
         $CI->load->library('dbconfiguration');
		
		 $modulos=$CI->dbconfiguration->getModules();
		 $tablas=$this->getAllTable();
		 for($i=0;$i<count($modulos);$i++){
			 for($j=0;$j<count($tablas);$j++){
			 	if($modulos[$i]==$tablas[$j]){
					$this->execute($tablas[$j]);
					}
			 }
			 }
		 
		 
		 
		 
		 
	}
	 
	 function execute($tabla){
		 $t=$this->obtenerTabla($tabla);
		 
		 
		 
		 ####################
		 $this->phpConstructor['tabla']=$tabla;
		 
		 
		 $this->phpService['tabla']=$tabla;
		 
		 }
	 
	 function createFile(){
		 $constructFile = fopen("construct/templateconstruct.txt", "r");
		 $serviceFile= fopen("construct/templateservice.txt","r");
		 
		 #####ConstructFile
		 $constructFile=str_replace('@{tabla}',$this->phpConstructor['tabla'],$constructFile);
		 $constructFile=str_replace('@{cabecerasTabla}',$this->phpConstructor['cabecerasTabla'],$constructFile);
		 $constructFile=str_replace('@{valoresTabla}',$this->phpConstructor['valoresTabla'],$constructFile);
		 $constructFile=str_replace('@{validaciones}',$this->phpConstructor['validaciones'],$constructFile);
		 $constructFile=str_replace('@{selectValores}',$this->phpConstructor['selectValores'],$constructFile);
		 $constructFile=str_replace('@{form}',$this->phpConstructor['form'],$constructFile);
		 $constructFile=str_replace('@{otherService}',$this->phpConstructor['otherService'],$constructFile);
		 $constructFile=str_replace('@{inicializacion}',$this->phpConstructor['inicializacion'],$constructFile);
		 $constructFile=str_replace('@{inicializacionEditDelete}',$this->phpConstructor['inicializacionEditDelete'],$constructFile);
		 $constructFile=str_replace('@{saveAddEdit}',$this->phpConstructor['saveAddEdit'],$constructFile);
		 $constructFile=str_replace('@{saveDelete}',$this->phpConstructor['saveDelete'],$constructFile);
		 
		 #####ServiceFile
		 $serviceFile=str_replace('@{tabla}',$this->phpService['tabla'],$serviceFile);
		 $serviceFile=str_replace('@{dqlReturnData}',$this->phpService['dqlReturnData'],$serviceFile);
		 $serviceFile=str_replace('@{dqlReturnSelect}',$this->phpService['dqlReturnSelect'],$serviceFile);
		 $serviceFile=str_replace('@{dqlReturnList}',$this->phpService['dqlReturnList'],$serviceFile);
		 
		 ###### Creacion de archivos
		 
			$constructPhp = fopen('Construct'.$this->phpService['tabla'].'.php', "w+");
			fwrite($constructPhp,$constructFile);
			fclose($constructPhp);
			
			$servicePhp = fopen('Service'.$this->phpService['tabla'].'.php', "w+");
			fwrite($servicePhp,$serviceFile);
			fclose($servicePhp);
			
			
		 }
		 
		 
	 function templateService(){
		 $this->htmlService='';
		 
		 }
		 
		 
		 
	########################################
	
	function getAllTable(){
	 	$CI =& get_instance();
	 	$CI->load->database();
	 	
	 	
	 	$consulta=$CI->db->query("SELECT table_name FROM information_schema.columns");
	 	return $consulta->result_array();
	 	
	 }
	 
	 function getAllColumnName(){
	 	$CI =& get_instance();
	 	$CI->load->database();
	 	require APPPATH . 'config/database.php';
		 $consulta=$CI->db->query("SELECT distinct column_name FROM information_schema.columns  where table_schema='".$db['default']['database']."'");
	 	return $consulta->result_array();
		 }
	 
	 function obtenerTabla($tabla){
	 	$CI =& get_instance();
	 	$CI->load->database();
	 	
	 	$consulta=$CI->db->query("SELECT column_name, data_type,column_type, column_key, is_nullable FROM information_schema.columns where table_name='".$tabla."'");
	 	return $consulta->result_array();
	 	
	 }
	 
	 function generarMapeoxTabla($tabla){
	 	$info=$this->obtenerTabla($tabla);
	 	return $info;
	 }
	 
	 
	 function construyeForm($tabla,$listaIgnorar=NULL){
	 	$campos=$this->generarMapeoxTabla($tabla);
	 	
	 	for($i=0;$i<count($campos);$i++){
	 		$campos[$i]=$this->createcampos($campos[$i],$tabla);
	 		
	 	}
	 	return $campos;
	 
	 }
	 
	 function createSelect(){
	 	
	 	
	 }
	 
	 function createCampos($campo,$tabla){
	 	
	 	if($campo['column_name'][0]=='id'.$tabla) {$campo['type']='hidden';}
	 	elseif($campo['column_name'][0]=='i' && $campo['column_name'][1]=='d') {
	 		$campo['type']='select';
	 		$t=str_replace('id','',$campo['column_name']);
	 		$campo['tabla_select']=$t;
	 		
	 	}
	 	elseif($campo['column_name']=='eliminado'){ $campo['type']='NULL';}
	 	elseif($campo['column_name']=='estado'){ $campo['type']='select';}
	 	elseif($campo['column_name']=='email'){ $campo['type']='email';}
	 	elseif($campo['data_type']=='datetime'){ $campo['type']='date';}
	 	elseif($campo['column_name']=='password'){ $campo['type']='password';}
	 	elseif($campo['data_type']=='int'){ $campo['type']='select';}
	 	elseif($campo['data_type']=='varchar'){ $campo['type']='text';}
	 	elseif($campo['data_type']=='url'){ $campo['type']='url';}
	 	elseif($campo['data_type']=='imagen'){ $campo['type']='file';}
	 	
	 	
	 	if($campo['is_nullable']=='YES') $campo['validacion'][0]='NULL'; 
	 	else{
	 		if($campo['data_type']=='varchar'){
	 			$cadena=str_replace('varchar(','',$campo['column_key']);
	 			$cadena=str_replace(')','',$cadena);
	 			$cantidad=intval($cadena);
	 			$campo['validacion'][0]='NOT NULL';
	 			$campo['validacion'][1]='MAX'.$cantidad;
	 		}
	 		
	 		if($campo['data_type']=='datetime'){
	 			$campo['validacion'][0]='NOT NULL';
	 		}
	 		if($campo['data_type']=='int'){
	 			
	 			$campo['validacion'][0]='NOT NULL';
	 			$campo['validacion'][1]='NUMBER';
	 		}
	 		
	 		if($campo['data_type']=='float'){
	 				
	 			$campo['validacion'][0]='NOT NULL';
	 			$campo['validacion'][1]='FLOAT';
	 		}
	 		
	 	}
	 	return $campo;
	 	
	 }
	 	 
		 
		 
	 
}
?>